import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { LevelLimitDefin } from "../../common/defin/LevelLimitDefin";
import { GameProxy } from "../mvc/GameProxy";
import { DataEvent } from "../events/DataEvent";
import { LGXBViewHanderType } from "../view/LGXB/LGXBViewHanderType";
import { TaskInfo } from "../model/TaskInfo";
import { RewardUtil } from "../view/common/util/RewardUtil";
import { Language } from "../../../extend/language/Language";
import { STask } from "../../../Message/Game/STask";
import { SReward } from "../../../Message/Public/SReward";
import { ETaskStatus } from "../../../Message/Game/ETaskStatus";
import { LoaderHelp } from "../../common/display/LoaderHelp";
import { ResFileConst } from "../resource/ResFileConst";
import { SDragonPosInfo } from "../../../Message/Public/SDragonPosInfo";
import { LGXBModule } from "../view/LGXB/LGXBModule";
type int = number;
//class LGXBController
    
    export  class LGXBController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.LGXBOpeng, this.showModule);
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.taskEndHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskCancelRes, this.taskCancelHander);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.taskMsgHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskDel, this.taskMsgHandler);
            NetDispatcher.addCmdListener(ServerCommand.ResLGXBDicCount, this.resLGXBDicCountHandler);
            NetDispatcher.addCmdListener(ServerCommand.LGXBGetRewardUpdate, this.resLGXBRewardMsgHandler);
            NetDispatcher.addCmdListener(ServerCommand.LGXBDragonGetPosInfo, this.dragonGetPosInfoHander);
            NetDispatcher.addCmdListener(ServerCommand.LGXBGetRoadArr, this.goRoadsHander);
            RolePlayer.instance.addEventListener(PlayerEvent.UpdateEquip, this.playerEquipChange);
            Dispatcher.addEventListener(EventName.LGXBGetNextPointTimerOut, this.reqNextPointHandler);
            Dispatcher.addEventListener(EventName.LGXBtoGetRoads, this.toGetRoadsHander);
            Dispatcher.addEventListener(EventName.Scene_Update, this.sceneFirstUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.roleLevelUpdateHandler);
            return;
        }

        /* internal  */roleLevelUpdateHandler(arg1: Object): void
        {
            if (cache.role.roleInfo.level == LevelLimitDefin.LGXBTaskMinLevel) 
            {
                GameProxy.lgxb.getDicCount();
            }
            return;
        }

        /* internal  */sceneFirstUpdateHandler(arg1: DataEvent): void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.sceneFirstUpdateHandler);
            if (cache.role.roleInfo.level >= LevelLimitDefin.LGXBTaskMinLevel) 
            {
                GameProxy.lgxb.getDicCount();
            }
            return;
        }

        /* internal  */playerEquipChange(arg1: Object): void
        {
            if (this._moduleViewer) 
            {
                this._moduleViewer.viewerHander(null, LGXBViewHanderType.playerEquipChange);
            }
            return;
        }

        /* internal  */taskCancelHander(arg1: Object): void
        {
            this.taskMsgHandler(arg1);
            if (this._moduleViewer) 
            {
                this._moduleViewer.viewerHander(null, LGXBViewHanderType.taskIsFinish);
            }
            return;
        }

        /* internal  */toGetRoadsHander(arg1: Object /* flash.events.Event */): void
        {
            GameProxy.lgxb.dragonGo();
            return;
        }

        /* internal  */goRoadsHander(arg1: Array<any>): void
        {
            GameProxy.lgxb.getDicCount();
            if (this._moduleViewer) 
            {
                this._moduleViewer.viewerHander(null, LGXBViewHanderType.playerToTargetPoints, arg1);
            }
            return;
        }

        /* internal  */reqNextPointHandler(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */onTimeToGetDicCount(): void
        {
            this._lastTimerId = -1;
            GameProxy.lgxb.getDicCount();
            return;
        }

        /* internal  */taskEndHandler(arg1: Object): void
        {
            if (!this.isLGXBTask(arg1)) 
            {
                return;
            }
            var loc1=arg1.task as TaskInfo;
            var loc2=loc1.sTask.rewards;
            if (loc2 == null || loc2.length == 0) 
            {
                return;
            }
            var loc3=RewardUtil.getStrByRewardArray(loc2, "、");
            loc3 = Language.getStringByParam(80276, loc3);
            cache.lgxb.addRewardsMsg(loc3);
            this.taskMsgHandler(arg1);
            if (this._moduleViewer) 
            {
                this._moduleViewer.viewerHander(null, LGXBViewHanderType.taskIsFinish);
            }
            GameProxy.lgxb.getDicCount();
            return;
        }

        /* internal  */onTaskGetHandler(arg1: Object): void
        {
            if (!this.isLGXBTask(arg1)) 
            {
                return;
            }
            this.taskMsgHandler(arg1);
            if (this._moduleViewer) 
            {
                this._moduleViewer.viewerHander(null, LGXBViewHanderType.taskIsOn);
            }
            return;
        }

        /* internal  */isLGXBTask(arg1: Object): Boolean
        {
            if (arg1 == null) 
            {
                return false;
            }
            var loc1=arg1.task as TaskInfo;
            if (loc1 == null) 
            {
                return false;
            }
            if (loc1.sTask.group != this.taskGroup) 
            {
                return false;
            }
            return true;
        }

        /* internal  */taskMsgHandler(arg1: Object): void
        {
            var loc1=null;
            if (arg1 == null) 
            {
                return;
            }
            if (typeof arg1 === "stask") 
            {
                if (arg1.group != this.taskGroup) 
                {
                    return;
                }
            }
            else 
            {
                if (typeof arg1 === "array") 
                {
                    loc1 = arg1[0];
                }
                else 
                {
                    loc1 = arg1.task;
                }
                if (loc1 == null) 
                {
                    return;
                }
                if (loc1.sTask.group != this.taskGroup) 
                {
                    return;
                }
            }
            this.checkAndSetTaskStatus();
            this.refleshTopAndRightWin();
            return;
        }

        /* internal  */resLGXBDicCountHandler(arg1: Object): void
        {
            this.reflashDescTopWin();
            var loc1=cache.lgxb.dicCountInfo;
            if (loc1 != null) 
            {
                if (this._lastTimerId > 0) 
                {
                    flash.utils.clearTimeout(this._lastTimerId);
                }
                this._lastTimerId = flash.utils.setTimeout(this.onTimeToGetDicCount, (loc1.addTime + 1) * 1000);
                Dispatcher.dispatchEvent(new DataEvent(EventName.LGXBDicCountChange, loc1.count));
            }
            return;
        }

        /* internal  */resLGXBRewardMsgHandler(arg1: Object): void
        {
            if (this._moduleViewer) 
            {
                this._moduleViewer.viewerHander(null, LGXBViewHanderType.showDiceStarBtn);
            }
            if (arg1 == null) 
            {
                return;
            }
            GameProxy.lgxb.getDicCount();
            var loc1=arg1 as SReward;
            if (loc1 != null) 
            {
                cache.lgxb.addParseAndAddRewardMsg(loc1);
                this.reflashDescTopWin();
            }
            return;
        }

        /* internal  */reflashDescTopWin(): void
        {
            var loc1=0;
            var loc2=60;
            var loc3=cache.lgxb.dicCountInfo;
            if (loc3 != null) 
            {
                loc1 = loc3.count;
                loc2 = loc3.addTime;
            }
            var loc4=[loc1, cache.lgxb.rewardsMsgs, loc2, 20];
            if (this._moduleViewer != null) 
            {
                this._moduleViewer.viewerHander(null, LGXBViewHanderType.updateTopDescWin, loc4);
            }
            return;
        }

        /* internal  */checkAndSetTaskStatus(): void
        {
            var loc1=cache.task.getTaskByGroup(this.taskGroup, ETaskStatus._ETaskStatusNotCompleted).concat(cache.task.getTaskByGroup(this.taskGroup, ETaskStatus._ETaskStatusHadCompleted));
            if (!(loc1 == null) && loc1.length > 0) 
            {
                cache.lgxb.curTaskInfo = loc1[0];
            }
            else 
            {
                cache.lgxb.curTaskInfo = null;
            }
            return;
        }

        /* internal  */onModuleAddToStage(arg1: Object /* flash.events.Event */): void
        {
            this.checkAndSetTaskStatus();
            this.refleshTopAndRightWin();
            if (cache.lgxb.curTaskInfo != null) 
            {
                if (this._moduleViewer) 
                {
                    this._moduleViewer.viewerHander(null, LGXBViewHanderType.taskIsOn);
                }
            }
            GameProxy.lgxb.getDicCount();
            return;
        }

        /* internal  */refleshTopAndRightWin(): void
        {
            var loc1=null;
            if (this._moduleViewer == null) 
            {
                return;
            }
            this.reflashDescTopWin();
            if (cache.lgxb.curTaskInfo == null) 
            {
                loc1 = [false, null, Language.getString(80281), Language.getString(80282)];
            }
            else 
            {
                loc1 = [true, cache.lgxb.curTaskInfo, Language.getString(80281), Language.getString(80282)];
            }
            this._moduleViewer.viewerHander(null, LGXBViewHanderType.updateRightDescWin, loc1);
            return;
        }

        /* internal  */showModule(arg1: Object /* flash.events.Event */): void
        {
            this.checkAndSetTaskStatus();
            if (!this.hasGetLoginInfo) 
            {
                GameProxy.lgxb.dragonGetPosInfo();
            }
            LoaderHelp.addResCallBack(ResFileConst.LGXB, this.onUISourceLoadedHandler);
            return;
        }

        /* internal  */dragonGetPosInfoHander(arg1: SDragonPosInfo): void
        {
            this.hasGetLoginInfo = true;
            if (!cache.lgxb.loginInfo.getReward) 
            {
                GameProxy.lgxb.personOnReach();
            }
            this.checkToShow();
            return;
        }

        /* internal  */onUISourceLoadedHandler(): void
        {
            this.hasLoadUISource = true;
            this.checkToShow();
            return;
        }

        /* internal  */checkToShow(): void
        {
            if (!this.hasGetLoginInfo || !this.hasLoadUISource) 
            {
                return;
            }
            if (!this._moduleViewer) 
            {
                this._moduleViewer = new LGXBModule();
                this._moduleViewer.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onModuleAddToStage);
                this._moduleViewer.viewerHander(null, LGXBViewHanderType.gameInit);
            }
            this._moduleViewer.show();
            return;
        }

        private /* var */hasLoadUISource: Boolean;

        private /* var */hasGetLoginInfo: Boolean;

        private /* var */_moduleViewer: LGXBModule;

        private /* var */taskGroup: int=20;

        private /* var */_lastTimerId: int;
    }
